/*
 *    Description: ipupdate managemnt
 *          Author:  dimmalex (dim), dimmalex@gmail.com
 *      Company:  HP
 */
 
#include "land/farm.h"



const char *_intro( obj_t this )
{
    const char *str = \
"{"\
"\n    \"setup\":\"setup the ipupdate\","\
"\n    \"shut\":\"shutdown the ipupdate\""\
"\n}\n";
    return str;
}
talk_t _setup( obj_t this, param_t param )
{
    talk_t cfg;
    struct stat st;
    const char *ptr;

    /* ipupdate check */
    if ( stat( "/usr/bin/ez-ipupdate", &st ) != 0 )
    {
        return false;
    }
    cfg = config_sget( COM_IDPATH, NULL );
    ptr = json_get_string( cfg, "status" );
    if ( ptr == NULL || 0 != strcmp( ptr, "enable" ) )
    {
        talk_free( cfg );
        return tfalse;
    }
    serv_start( this, "service", NULL, COM_IDPATH );
    talk_free( cfg );
    return ttrue;
}
talk_t _shut( obj_t this, param_t param )
{
    serv_stop( COM_IDPATH );
    return ttrue;
}
talk_t _service( obj_t this, param_t param )
{
    talk_t cfg;
	const char *ptr;
    const char *ifname;
    const char *isp;
    const char *account;
    const char *password;
    const char *domain;
    char username[COMMAND_SIZE];
    char connect_interface[IDENTIFY_SIZE];
    char connect_ip[IDENTIFY_SIZE];

    cfg = config_sget( COM_IDPATH, NULL );
    /* get the ext ifname */
    memset( connect_ip, 0, sizeof(connect_ip) );
    memset( connect_interface, 0, sizeof(connect_interface) );
    ifname = json_get_string( cfg, "extern" );
    if ( ifname != NULL && *ifname != '\0' )
    { 
        ptr = com_scalls( ifname, "device", NULL, connect_interface, sizeof(connect_interface) );
        if ( ptr == NULL )
        {
            fault( "no extern interface" );
            talk_free( cfg );
            sleep( 5 );
            return tfalse;
        }
        if ( netcard_info( ptr, connect_ip, sizeof(connect_ip), NULL, 0, NULL, 0, NULL, 0 ) != 0 )
        {
            fault( "no %s ip address", ptr );
            talk_free( cfg );
            sleep( 5 );
            return tfalse;
        }
    }
    else
    {
        if ( route_exist( "0.0.0.0", NULL, NULL, connect_interface ) == false )
        {
            fault( "no gateway route" );
            talk_free( cfg );
            sleep( 5 );
            return tfalse;
        }
        if ( netcard_info( connect_interface, connect_ip, sizeof(connect_ip), NULL, 0, NULL, 0, NULL, 0 ) != 0 )
        {
            fault( "no gateway ip address" );
            talk_free( cfg );
            sleep( 5 );
            return tfalse;
        }
    }
    account = json_get_string( cfg, "account" );
    if ( account == NULL )
    {
        fault( "no ipupdate configure" );
        talk_free( cfg );
        sleep( 5 );
        return tfalse;
    }
    isp = json_get_string( cfg, "isp" );
    password = json_get_string( cfg, "password" );
    domain = json_get_string( cfg, "domain" );
    snprintf( username, sizeof( username ) - 1, "%s:%s", account, password);

    execlp( "ez-ipupdate", "ez-ipupdate", "-S", isp, "-i", connect_interface, "-u", username, "-h", domain, "-w", "/tmp/.ez-ipupdate.cache", (char *)0 );
    faulting( "execlp the ez-ipupdate error" );

    talk_free( cfg );
    return tfalse;
}



boole _set( obj_t this, path_t path, talk_t v )
{
    obj_t o;
    boole ret;

    o = obj_create( COM_IDPATH );
    ret = config_set( o, path, v );
    if ( ret == true )
    {
        _shut( this, NULL );
        _setup( this, NULL );
    }
    obj_free( o );
    return ret;
}
talk_t _get( obj_t this, path_t path )
{
    obj_t o;
    talk_t ret;

    o = obj_create( COM_IDPATH );
    ret = config_get( o, path );
    obj_free( o );
    return ret;
}



